package com.log6z.dashborad.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.log6z.dashborad.domain.Appointment;
import com.log6z.dashborad.domain.LineQueue;
import com.log6z.dashborad.mapper.AppointmentMapper;
import com.log6z.dashborad.mapper.LineQueueMapper;
import com.log6z.dashborad.service.AppointmentService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lu-profiess
 * @description 针对表【appointment(预约表)】的数据库操作Service实现
 * @createDate 2023-12-29 19:04:48
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private LineQueueMapper lineQueueMapper;
    @Override
    public HashMap<String, Object> baseData() {
        List<Integer> integers = appointmentMapper.realTimeReal();
        List<Double> saleCoalTon = appointmentMapper.totalSaleCoalTon();
        /**
         * 查询近三日的数据
         * 预约 打卡 叫号 作业 完成
         */


        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            DateTime offset = DateUtil.offset(DateUtil.date(), DateField.DAY_OF_MONTH, -i);
            String key = DateUtil.format(offset, "MM-dd");
            HashMap<String, Object> dayMap = new HashMap<>();
            dayMap.put("title",key);

            List<Appointment> list = appointmentMapper.appointmentTimeCount(DateUtil.beginOfDay(offset), DateUtil.endOfDay(offset));
            dayMap.put("appointmentCount",list.stream().mapToInt(Appointment::getTotalCount).sum());
            dayMap.put("appointmentCountList",list);
            list = appointmentMapper.clockTimeCount(DateUtil.beginOfDay(offset), DateUtil.endOfDay(offset));
            dayMap.put("clickCount",list.stream().mapToInt(Appointment::getTotalCount).sum());
            dayMap.put("clickCountList",list);
            list = appointmentMapper.callNumberTimeCount(DateUtil.beginOfDay(offset), DateUtil.endOfDay(offset));
            dayMap.put("callCount",list.stream().mapToInt(Appointment::getTotalCount).sum());
            dayMap.put("callCountList",list);
            list = appointmentMapper.tareWeighTimeCount(DateUtil.beginOfDay(offset), DateUtil.endOfDay(offset));
            dayMap.put("workedCount",list.stream().mapToInt(Appointment::getTotalCount).sum());
            dayMap.put("workedCountList",list);
            list = appointmentMapper.weighTimeCount(DateUtil.beginOfDay(offset), DateUtil.endOfDay(offset));
            dayMap.put("finishCount",list.stream().mapToInt(Appointment::getTotalCount).sum());
            dayMap.put("finishCountList",list);


            arrayList.add(dayMap);

        }

        List<LineQueue> lineQueues = lineQueueMapper.dayLineCount();
        HashMap<String, Object> returnHash = new HashMap<>();
        returnHash.put("dayClickDataList", arrayList);
        returnHash.put("realTimeDoingData", integers);
        returnHash.put("queueLineList", lineQueues);
        returnHash.put("totalTon", saleCoalTon);
        return returnHash;
    }


    @Override
    public HashMap chartEcData(Integer custId) {
        HashMap<Object, Object> returnHasMap = new HashMap<>();
        List<HashMap<String,Integer>> ocarrierApproval = appointmentMapper.carrierApproval();
        this.zuzhuangData(ocarrierApproval,returnHasMap,"carrierApproval");
        List<HashMap<String,Integer>> callTimeToweighTime = appointmentMapper.callTimeToInParkTime();
        this.zuzhuangData(callTimeToweighTime,returnHasMap,"callTimeToweighTime");
        List<HashMap<String,Integer>> tareWeighTimeToweighTime = appointmentMapper.tareWeighTimeToWeighTime();
        this.zuzhuangData(tareWeighTimeToweighTime,returnHasMap,"tareWeighTimeToweighTime");
        List<HashMap<String,Integer>> inParkAndOutHour = appointmentMapper.inParkAndOutHour();
        this.zuzhuangData(inParkAndOutHour,returnHasMap,"inParkAndOutHour");
        List<HashMap<String, Integer>> queueWeekChart = lineQueueMapper.weekCount();
        this.zuzhuangData(queueWeekChart,returnHasMap,"queueWeekChart");
        /**
         * 近15日 - 每日的的出矿 量（吨）
         */
        List<HashMap<String, Double>> hashMaps = appointmentMapper.outTon15Day(custId);
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> yList = new ArrayList<>();
        for (HashMap<String, Double> hashMap : hashMaps) {
            xList.add(hashMap.get("x"));
            yList.add(hashMap.get("y"));
        }
        HashMap<String, List> stringListHashMap = new HashMap<>();
        stringListHashMap.put("x",xList);
        stringListHashMap.put("y",yList);
        returnHasMap.put("outTon15Day",stringListHashMap);
        /**
         * 近15日-拉运过的客户，用于筛选
         */
        ArrayList<Object> custList = new ArrayList<>();
        List<Appointment> appointments = appointmentMapper.outTon15DayCustInfo();
        for (Appointment appointment : appointments) {
            HashMap<String, Object> custMap = new HashMap<>();

            custMap.put("custId",appointment.getCustId());
            custMap.put("custName",appointment.getCustName());
            custList.add(custMap);
        }
        returnHasMap.put("custList",custList);
        return returnHasMap;
    }

    private void zuzhuangData(List<HashMap<String,Integer>> list,HashMap<Object, Object> returnHasMap,String mapKey){
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> yList = new ArrayList<>();
        ArrayList<Object> y1List = new ArrayList<>();
        for (HashMap<String, Integer> hashMap : list) {
            xList.add(hashMap.get("date"));
            yList.add(hashMap.get("min1"));
            y1List.add(hashMap.get("min2"));
        }
        HashMap<String, List> stringListHashMap = new HashMap<>();
        stringListHashMap.put("x",xList);
        stringListHashMap.put("y",yList);
        stringListHashMap.put("y1",y1List);
        returnHasMap.put(mapKey,stringListHashMap);
    }
}




